package org.alex.samples.dao;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.alex.samples.model.ExamScore;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

@Repository
@Transactional
public class ExamScoreDAOImpl extends BaseHibernateDAO<ExamScore> implements IExamScoreDAO{

	@Autowired
	IUserDAO userdao;
	
	@Override
	public ExamScore getScoreByUIdSId(Integer userid, Integer subjectid) {
		String hql=" FROM ExamScore where user_id = ? and subject_id = ? ";
		ExamScore examScore = queryForObject(hql, new Object[]{userid,subjectid});
		return examScore;
	}

	@Override
	public void saveScoreByUIdSId(Integer userid, Integer subjectid , Integer score) {
		ExamScore examScore = this.getScoreByUIdSId(userid, subjectid);
		examScore.setScore(score);
		examScore.setExamTime(new Date());
		this.save(examScore);
	}

	@Override
	public List<ExamScore> getAllScore() {
		String hql = " FROM ExamScore ";
		return queryForList(hql, null );
	}

	@Override
	public List<ExamScore> searchScore(Integer subjectId, Integer studentId, String studentName, String testDate) {
		List<ExamScore> examScore2 = getAllScore();
		List<ExamScore> examScore1 = new ArrayList<>();
		for(ExamScore s :examScore2){
			if(s.getScore()!=null){
				examScore1.add(s);
			}
		}
		examScore2 = new ArrayList<>();
		
		//验证subjectId

		if(subjectId == null){
			examScore2 = examScore1;
		}else{
			for(ExamScore s:examScore1){
				if(s.getSubjectId() == subjectId){
					examScore2.add(s);
				}
			}
		}
		
		//清空

		examScore1 = new ArrayList<>();
		
		//验证studentId

		if(studentId == null){
			examScore1 = examScore2;
		}else{
			for(ExamScore s:examScore2){
				if(s.getUserId() == studentId){
					examScore1.add(s);
				}
			}
		}
		
		//清空

		examScore2 = new ArrayList<>();
		
		//验证studentName

		if(studentName.equals("")){
			examScore2 = examScore1;
		}else{
			for(ExamScore s:examScore1){
				if(userdao.get(s.getUserId()).getName().equals(studentName)){
					examScore2.add(s);
				}
			}
		}
		
		//清空

		examScore1 = new ArrayList<>();
		//验证testDate

		if(testDate.equals("")){
			examScore1 = examScore2;
		}else{
			for(ExamScore s:examScore2){
				Date examtime = s.getExamTime();
				SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
				String d = sdf.format(examtime);
				if(d.equals(testDate)){
					examScore1.add(s);
				}
			}
		}
		
		
		return examScore1;
	}

	@Override
	public ExamScore getScoreById(Integer id) {
		return this.get(id);
	}

	@Override
	public void updateScore(ExamScore s) {
		this.update(s);
	}

	@Override
	public void deleteScore(ExamScore s) {
		ExamScore e = this.get(s.getId());
		if(e != null){
			this.delete(e);
		}
	}
	
}
